package com.zhoukx.nio;

import java.nio.ByteBuffer;

/**
 * 可以将一个普通Buffer 转成只读Buffer [举例说明]
 */
public class ReadOnlyBuffer {
    public static void main(String[] args) {
        // 创建一个缓存
        ByteBuffer allocate = ByteBuffer.allocate(64);

        for (int i = 0; i < 64; i++) {
            allocate.put((byte) i);
        }
        allocate.flip();
        //得到一个只读的buffer
        ByteBuffer byteBuffer = allocate.asReadOnlyBuffer();
        System.out.println();
        while (byteBuffer.hasRemaining()) {
            System.out.println(byteBuffer.get());
        }

        byteBuffer.put((byte)100);
    }
}
